/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package stis.statpro.ibs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import stis.statpro.ibs.datasource.Datasource;
import stis.statpro.ibs.model.Kabupaten;
import stis.statpro.ibs.model.Kecamatan;

/**
 *
 * @author habib <imamhabibpamungkas@gmail.com>
 */
public class KecamatanDao {


    public List<String> getALl(int idKab) throws SQLException {
        List<String> listString = new ArrayList<>();
        Connection c = Datasource.getConnection();
        PreparedStatement p = c.prepareStatement("SELECT * from kecamatan where id_kabupaten=?;");
        p.setInt(1, idKab);
        ResultSet rs = p.executeQuery();
        while (rs.next()) {
            listString.add(rs.getString("nama"));
        }
        p.close();
        return listString;
    }
    
    public List<Kecamatan> getId(int nama) throws SQLException{
        List<Kecamatan> listKec = new ArrayList<>();
        Connection c = Datasource.getConnection();
        PreparedStatement p = c.prepareStatement("SELECT * from Kecamatan where id_Kabupaten=?;");
        p.setInt(1, nama);
        ResultSet rs = p.executeQuery();
        while (rs.next()) {
            Kecamatan kec = new Kecamatan();
            kec.setId(rs.getInt("id"));
            kec.setIdKabupaten(rs.getInt("id_kabupaten"));
            kec.setNama(rs.getString("nama"));
            listKec.add(kec);
        }
        p.close();
        return listKec;
    }
}
